package edu.iyte.ceng316;

import java.awt.EventQueue;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTextField;


import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JListBinding;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import edu.iyte.ceng316.model.Person;
import edu.iyte.ceng316.model.PhoneGroup;
import edu.iyte.ceng316.model.data.PhoneBookManager;
import edu.iyte.ceng316.model.data.PhoneBookSingleton;
import edu.iyte.ceng316.ui.phonebook.mediator.PhoneBookMediatorImpl;

public class MainFrame extends JFrame{
	
	MainSplitPane msp;
	PhoneBookManager modelManager;
	PhoneBookMediatorImpl phoneBookMediatorImpl;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame(); 
					frame.setTitle("Phone Book Example");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainFrame() {
		modelManager = (PhoneBookManager) PhoneBookSingleton.getModelManager();
		modelManager.initialize();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 750, 400);
		msp = new MainSplitPane();
		setContentPane(msp);
		
		phoneBookMediatorImpl = new PhoneBookMediatorImpl();
		phoneBookMediatorImpl.setLeftPanel(msp.getLeftPanel());
		phoneBookMediatorImpl.setPersonInfoPanel(msp.getRightPanel().getTopPanel().getPersonInfoPanel());
		
		msp.setIPhoneBookMediator(phoneBookMediatorImpl);
		initDataBindings();
		/*
		 * Test
		 */
	}
	protected void initDataBindings() {
		BeanProperty<PhoneBookManager, List<PhoneGroup>> phoneBookManagerBeanProperty = BeanProperty.create("groups.groups");
		JListBinding<PhoneGroup, PhoneBookManager, JList> jListBinding = SwingBindings.createJListBinding(UpdateStrategy.READ, modelManager, phoneBookManagerBeanProperty, msp.getLeftPanel().getDepartmentList());
		//
		ELProperty<PhoneGroup, Object> phoneGroupEvalutionProperty = ELProperty.create("${name} (${personCount})");
		jListBinding.setDetailBinding(phoneGroupEvalutionProperty);
		//
		jListBinding.bind();
		//
		BeanProperty<JList, List<Person>> jListPersonsBeanProperty = BeanProperty.create("selectedElement.persons");
		JTableBinding<Person, JList, JTable> jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ, msp.getLeftPanel().getDepartmentList(), jListPersonsBeanProperty, msp.getRightPanel().getTopPanel().getPersonInfoPanel().getTable());
		//
		BeanProperty<Person, String> personBeanProperty = BeanProperty.create("name");
		jTableBinding.addColumnBinding(personBeanProperty).setColumnName("Name");
		//
		BeanProperty<Person, String> personBeanProperty_1 = BeanProperty.create("email");
		jTableBinding.addColumnBinding(personBeanProperty_1).setColumnName("E-mail");
		//
		BeanProperty<Person, String> personBeanProperty_2 = BeanProperty.create("phone");
		jTableBinding.addColumnBinding(personBeanProperty_2).setColumnName("Phone");
		//
		BeanProperty<Person, String> personBeanProperty_3 = BeanProperty.create("mobilePhone1");
		jTableBinding.addColumnBinding(personBeanProperty_3).setColumnName("Mobile Phone 1");
		//
		BeanProperty<Person, String> personBeanProperty_4 = BeanProperty.create("mobilePhone2");
		jTableBinding.addColumnBinding(personBeanProperty_4).setColumnName("Mobile Phone 2");
		//
		jTableBinding.setEditable(false);
		jTableBinding.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty = BeanProperty.create("selectedElement.name");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<JTable, String, JTextField, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, msp.getRightPanel().getTopPanel().getPersonInfoPanel().getTable(), jTableBeanProperty, msp.getRightPanel().getBottomPanel().getNameTextField(), jTextFieldBeanProperty);
		autoBinding.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty_1 = BeanProperty.create("selectedElement.email");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_1 = BeanProperty.create("text");
		AutoBinding<JTable, String, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, msp.getRightPanel().getTopPanel().getPersonInfoPanel().getTable(), jTableBeanProperty_1, msp.getRightPanel().getBottomPanel().getEmailTextField(), jTextFieldBeanProperty_1);
		autoBinding_1.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty_2 = BeanProperty.create("selectedElement.phone");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_2 = BeanProperty.create("text");
		AutoBinding<JTable, String, JTextField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, msp.getRightPanel().getTopPanel().getPersonInfoPanel().getTable(), jTableBeanProperty_2, msp.getRightPanel().getBottomPanel().getPhoneTextField(), jTextFieldBeanProperty_2);
		autoBinding_2.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty_3 = BeanProperty.create("selectedElement.mobilePhone1");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_3 = BeanProperty.create("text");
		AutoBinding<JTable, String, JTextField, String> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, msp.getRightPanel().getTopPanel().getPersonInfoPanel().getTable(), jTableBeanProperty_3, msp.getRightPanel().getBottomPanel().getMobilePhone1TextField(), jTextFieldBeanProperty_3);
		autoBinding_3.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty_4 = BeanProperty.create("selectedElement.mobilePhone2");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_4 = BeanProperty.create("text");
		AutoBinding<JTable, String, JTextField, String> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, msp.getRightPanel().getTopPanel().getPersonInfoPanel().getTable(), jTableBeanProperty_4, msp.getRightPanel().getBottomPanel().getMobilePhone2TextField(), jTextFieldBeanProperty_4);
		autoBinding_4.bind();
		//
		ELProperty<JList, Object> departmentListEvalutionProperty = ELProperty.create("${ selectedElement != null }");
		BeanProperty<JButton, Boolean> jButtonBeanProperty = BeanProperty.create("enabled");
		AutoBinding<JList, Object, JButton, Boolean> autoBinding_5 = Bindings.createAutoBinding(UpdateStrategy.READ, msp.getLeftPanel().getDepartmentList(), departmentListEvalutionProperty, msp.getLeftPanel().getEditDepartmentButton(), jButtonBeanProperty);
		autoBinding_5.bind();
		//
		ELProperty<JList, Object> jListEvalutionProperty = ELProperty.create("${ selectedElement != null }");
		AutoBinding<JList, Object, JButton, Boolean> autoBinding_6 = Bindings.createAutoBinding(UpdateStrategy.READ, msp.getLeftPanel().getDepartmentList(), jListEvalutionProperty, msp.getLeftPanel().getDeleteDepartmentButton(), jButtonBeanProperty);
		autoBinding_6.bind();
		//
		ELProperty<JTable, Object> jTableEvalutionProperty = ELProperty.create("${ selectedElement != null }");
		AutoBinding<JTable, Object, JButton, Boolean> autoBinding_7 = Bindings.createAutoBinding(UpdateStrategy.READ, msp.getRightPanel().getTopPanel().getPersonInfoPanel().getTable(), jTableEvalutionProperty, msp.getRightPanel().getTopPanel().getPersonButtonPanel().getDeletePersonButton(), jButtonBeanProperty);
		autoBinding_7.bind();
	}
}
